#ifndef DATABASE_HPP_INCLUDED
#define DATABASE_HPP_INCLUDED

#include <QtCore/QThread>
#include <QtCore/QString>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtLockedFile>
#include "model/AudioFile.hpp"

/** This class manges your DB operations, mainly queries. */
class Database : public QThread {
    Q_OBJECT
    private:
        /// @brief Used to lock the database to prevent writing from other programs
        QtLockedFile *lock;
        /// @brief File used as database
        QString file;
        QSqlDatabase *db;
        QSqlQuery querier;
        /* For other database implementation
        QString host, user, password;
        int port;*/
        /** Initialise database */
        void init();
        //install functions
        /// Install the database. It creates all elements from the database (tables, views, constraints, ...)
        void install();
    public:
        /** Erase all the database */
        void reset();
        /** Constructor, init the database
         * @param file: SQLite database file to sue
        */
        Database(QString file);
        /** Add or update a track to the database
         * @param track: AudioFile object containing data
        */
        void set(AudioFile *track);
        /** Add tag in the database */
        //unsigned short addTag(QString tag, QString type);
        /** Run an insert or update query
         * @param query: query to run
         * @param error: error message
        */
        //int insert_or_update(QString query, char *error);
        /** Delete a file from db
         * @param path: file path
        */
        void deleteTrack(AudioFile *af);
        QSqlDatabase* getDatabase();
        /// @brief May be used for big queries or to process a great amount results
        void run();
        ~Database();
        /** @brief Add the track to the database if it  not exists. If it is added, the track ID will be added into the AudioFile
         * @param af Audio file object
         * @return Track ID
         */
        unsigned int addTrack(AudioFile *af);
        /** @brief Add an artist. If the artist exists, it will return its ID
         * @param name Artist/group name
         * @param id Artist ID if name update
         * @return Artist ID
         */
        unsigned int addArtist(QString name, unsigned int id=0);
        /** @brief Add an album. If the album exists, it will return its ID
         * @param name Album name
         * @param id Album ID if name update
         * @param cds Number of CD
         * @param compilation If the album have more than one artist, it is a compilation
         * @return Album ID
         */
        unsigned int addAlbum(QString name, unsigned int id=0, unsigned short cds=1, bool compilation=false);
        /** @brief Add track to history
         * @param id ID of the track
         */
        void played(unsigned int id);
        //void tag(AudioFile af, QString tag);
        /** @brief Remove albums or/and artists without tracks */
        void cleanup();
    signals:
        void complete();
};

#endif // DATABASE_HPP_INCLUDED
